package main;

import javax.swing.*;
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
public class Main extends JFrame{

    protected int infWidth,infHeight;
    protected static  int width=Label.width*(Map.m+6),height=Label.height*(Map.n);

    protected static Map map=new Map();
    protected static JFrame frame=new JFrame();
    protected static Player me=new Player(1,1);
    protected static Container c=frame.getContentPane();
    protected static InfoBar infoBar=new InfoBar();
    public Main()throws InterruptedException{
        infHeight=Map.n;
        infWidth=6;
        width= Label.width*(Map.m+infWidth);
        height= Label.height*(Map.n);
        frame.setTitle("11Princess");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(width,height);

        c.setLayout(null);
        infoBar.addToFrame(c);
        //map.setPic(1,1,me.url);
        c.add(me.pic);
        //绘制地图
        for(int i=0;i<Map.n;i++){
            for(int j=0;j<Map.m;j++){
                c.add(map.pic[i][j]);
            }
        }

        map.setPic(5,5,Enemy.url);
        map.mp[5][5]=new Enemy(5,5);
        map.mp[5][5].hp=9;
        map.mp[5][5].atk=15;
        map.mp[5][5].dfc=2;
        map.mp[5][5].money=2;
        //绘制信息栏
        for(int i=0;i<infHeight;i++){
            for(int j=0;j<infWidth;j++){
                c.add(new Label(Rock.url,i,j+Map.m));
            }
        }

        frame.setVisible(true);
        frame.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                char cc=e.getKeyChar();

                switch (cc){
                    case 'w','W': me.pic.setIcon(new ImageIcon(me.url2));me.move(0,c); break;
                    case 'd','D': me.pic.setIcon(new ImageIcon(me.url4));me.move(1,c); break;
                    case 's','S': me.pic.setIcon(new ImageIcon(me.url1));me.move(2,c); break;
                    case 'a','A': me.pic.setIcon(new ImageIcon(me.url3));me.move(3,c); break;
                }
                update();
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        while(true){
            Thread.sleep(10);

        }
    }
    public static void update(){
        frame.revalidate();
        frame.repaint();
    }
    public static void main(String[] args) throws InterruptedException{
        new Main();
    }
}


class Label extends JLabel{
    //protected JLabel img=new JLabel();
    protected int x=0,y=0;

    public final static int width=55,height=55;
    public Label(String url, int x, int y){
        this.x=x;
        this.y=y;
        this.setIcon(new ImageIcon(url));
        this.setBounds(width*y,height*x,width,height);
        this.setOpaque(false);
    }

    public void update(String url){
        Main.c.remove(this);
        this.setIcon(new ImageIcon(url));
        Main.c.add(this);
    }
}

class Bar extends JPanel{
    protected JLabel txt;
    public Bar(String url,int i,int j){
        this.setLayout(new BorderLayout());
        this.add(new JLabel(new ImageIcon(url)),BorderLayout.WEST);
        txt=new JLabel("1211231");
        txt.setFont(new Font("黑体", Font.BOLD, 28));
        this.add(txt,BorderLayout.CENTER);
        this.setOpaque(false);
        this.setBounds((Map.m+j)* Label.width,(i)* Label.height, Label.width*3,Label.height);
    }
    public void setTxt(String s){
        txt.setText(s);
    }
}
class InfoBar{
    protected static int m=6;
    protected Bar hp;
    protected Bar atk;
    protected Bar dfc;

    protected Bar money;

    //protected Bar name;
    protected JPanel info;


    public InfoBar(){
        info=new JPanel();
        info.setOpaque(false);
        info.setLayout(null);
        info.setBounds(0,0, Main.width, Main.height);
        hp=new Bar("src/resources/floor.jpg",2,0);
        hp.setTxt(Main.me.hp.toString());
        atk=new Bar("src/resources/floor.jpg",2,3);
        atk.setTxt(Main.me.atk.toString());
        dfc=new Bar("src/resources/floor.jpg",3,0);
        dfc.setTxt(Main.me.dfc.toString());
        money=new Bar("src/resources/floor.jpg",3,3);
        money.setTxt(Main.me.money.toString());



    }
    public void addToFrame(Container c){
        c.add(info);
        c.add(hp);
        c.add(atk);
        c.add(dfc);
        c.add(money);
    }

    public void playerRefresh(){
        hp.setTxt(Main.me.hp.toString());
        atk.setTxt(Main.me.atk.toString());
        dfc.setTxt(Main.me.dfc.toString());
        money.setTxt(Main.me.money.toString());
    }
    public void showInfo(Item npc,int x,int y,Container c){
        info.removeAll();

        if(npc!=null){
            if(npc.type=="Enemy"){
                Bar eHp=new Bar("src/resources/floor.jpg",6,0);
                Bar eAtk=new Bar("src/resources/floor.jpg",6,3);
                Bar eDfc=new Bar("src/resources/floor.jpg",7,0);
                Bar eMoney=new Bar("src/resources/floor.jpg",7,3);
                Bar name=new Bar("src/resources/floor.jpg",5,0);

                name.setTxt(Main.me.hp.toString());
                eHp.setTxt(npc.hp.toString());
                eAtk.setTxt(npc.atk.toString());
                eMoney.setTxt(npc.money.toString());
                eDfc.setTxt(npc.dfc.toString());

                JButton bt=new JButton("攻打");
                bt.setBounds((Map.m+m/2-1)* Label.width,(12)* Label.height, Label.width*2, Label.height);
                bt.addActionListener(new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(Main.me.attack(npc,false)>0){
                            Main.me.attack(npc,true);
                            Main.map.mp[x][y]=new Floor(x,y);
                            Main.map.setPic(x,y,Floor.url);
                            System.out.println("攻打");
                            info.removeAll();
                            playerRefresh();
                            Main.update();
                            Main.frame.requestFocus();
                        }
                    }
                });


                info.add(bt);
                info.add(name);
                info.add(eHp);
                info.add(eAtk);
                info.add(eDfc);
                info.add(eMoney);
            }
            else{

            }
        }
        c.remove(info);
        c.add(info,0);
    }
}
class Map extends Item{
    public static Item mp[][];
    public static Label pic[][];
    public final static int n=16,m=20;

    public Map(){
        pic=new Label[n][m];
        mp=new Item[n][m];
        for(int i=0;i<n;i++){
            mp[i][0]=new Wall(i,0);
            pic[i][0]=new Label(Wall.url,i,0);
            mp[i][m-1]=new Wall(i,m-1);
            pic[i][m-1]=new Label(Wall.url,i,m-1);
            for(int j=1;j<m-1;j++){
                if(i==0||i==n-1){
                    mp[i][j]=new Wall(i,j);
                    pic[i][j]=new Label(Wall.url,i,j);
                }
                else{
                    mp[i][j]=new Floor(i,j);
                    pic[i][j]=new Label(Floor.url,i,j);
                }
            }
        }
    }
    public void setPic(int x,int y,String url){
        pic[x][y].update(url);
    }
}

